﻿using System;

using Gonte.Web.Json;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gonte.Web.Tests
{
    [TestClass()]
    public class JsonGridDataTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public class TestObject
        {
            public bool BoolProperty { get; set; }
            public int IntProperty { get; set; }
            public string StringProperty { get; set; }
            public DateTime DateTimeProperty { get; set; }
            public string[] StringArray { get; set; }
        }

        [TestMethod()]
        public void JsonGridDataPopulateSingleObjectTest()
        {
            JsonGridData data = new JsonGridData();
            object obj = new TestObject
            {
                BoolProperty = true,
                IntProperty = 5,
                StringProperty = "Text",
                DateTimeProperty = new DateTime(2002, 5, 25),
                StringArray = new string[] { "First", "Second", "Third"}
            };

            data.Populate(obj);

            Assert.AreEqual(obj, ((object[])data.gridData)[0]); // The object gets converted to an array if it is a single one

            JsonGridMetaData metadata = data.metaData;
            JsonFieldInfo[] fieldsInfo = metadata.fields;

            Assert.AreEqual("BoolProperty", fieldsInfo[0].name);
            Assert.AreEqual("bool", fieldsInfo[0].type);

            Assert.AreEqual("IntProperty", fieldsInfo[1].name);
            Assert.AreEqual("int", fieldsInfo[1].type);

            Assert.AreEqual("StringProperty", fieldsInfo[2].name);
            Assert.AreEqual("string", fieldsInfo[2].type);

            Assert.AreEqual("DateTimeProperty", fieldsInfo[3].name);
            Assert.AreEqual("date", fieldsInfo[3].type);

            Assert.AreEqual("StringArray", fieldsInfo[4].name);
            Assert.AreEqual("string", fieldsInfo[4].type);
        }
    }
}
